/**
 * 
 */
package cn.jhz.filesharingsystem.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jhz.filesharingsystem.model.Pager;
import cn.jhz.filesharingsystem.model.Role;
import cn.jhz.filesharingsystem.model.SystemContext;
import cn.jhz.filesharingsystem.model.User;
import cn.jhz.filesharingsystem.service.RoleService;
import cn.jhz.filesharingsystem.util.HeadersUtil;
import cn.jhz.filesharingsystem.web.AuthClass;
import cn.jhz.filesharingsystem.web.AuthMethod;

/**
 * @author asus
 *
 */
@AuthClass
@Controller
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@AuthMethod
	@GetMapping("/role_management")
	private String roleManagement(@RequestHeader(value="User-Agent") String userAgent) {
		if(HeadersUtil.checkAgentIsMobile(userAgent)) {
			return "mobileRoleManagement";
		}
		return "roleManagement";
	}

	@AuthMethod
	@PostMapping("/role")
	private @ResponseBody String addRole(@RequestBody Role role, HttpSession session) {
		try {
			User loginUser = (User)session.getAttribute("loginUser");
			if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
				throw new RuntimeException("帐号已被禁用");
			}
			roleService.add(role);
		}catch (Exception e) {
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@DeleteMapping("/roles")
	public @ResponseBody String deleteRoles(@RequestBody int[] ids, HttpSession session) {
		try {
			User loginUser = (User)session.getAttribute("loginUser");
			if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
				throw new RuntimeException("帐号已被禁用");
			}
			if(!loginUser.getRole().getRoleName().equals("super")) {
				throw new RuntimeException("权限不足");
			}
			for(int id:ids) {
				roleService.delete(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@PutMapping("/role")
	private @ResponseBody String updateRole(@RequestBody Role role, HttpSession session){
		try {
			User loginUser = (User)session.getAttribute("loginUser");
			if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
				throw new RuntimeException("帐号已被禁用");
			}
			roleService.update(role);
		}catch (Exception e) {
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@PutMapping("/roles")
	private @ResponseBody String updateState(HttpSession session,@RequestBody Integer...ids){
		try {
			User loginUser = (User)session.getAttribute("loginUser");
			if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
				throw new RuntimeException("帐号已被禁用");
			}
			for (Integer id : ids) {
				roleService.updateState(id);
			}
		}catch (Exception e) {
			return "error";
		}
		return "ok";
	}
	
	@AuthMethod
	@GetMapping("/roles_list")
	private @ResponseBody List<Map<String, String>> getRolesList(HttpSession session){

		List<Role> roles = null;
		List<Map<String, String>> roleList = new ArrayList<>();
		
		User loginUser = (User)session.getAttribute("loginUser");
		if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
			throw new RuntimeException("帐号已被禁用");
		}
		
		if(((boolean)session.getAttribute("isadmin"))) {
			roles = roleService.getAllRoles(false);
		}else{
			roles =roleService.getAllRoles(true);
		}
		
		
		
		for(Role role: roles) {
			if(!role.getRoleName().equals("super")) {
				Map<String, String> roleMap = new HashMap<>();
				roleMap.put("id", role.getId()+"");
				roleMap.put("text", role.getRoleName());
				roleList.add(roleMap);
			}
		}
		return roleList;
	}
	
	@AuthMethod	            
	@GetMapping("/roles")
	private @ResponseBody Pager<Role> getRolesPage(@RequestParam(value = "page", required = false)Integer page
			   ,@RequestParam(value = "rows", required = false) Integer rows
			   ,HttpSession session) {
		User loginUser = (User)session.getAttribute("loginUser");
		if(loginUser.getRole().getState()==0 || loginUser.getState()==0) {
			throw new RuntimeException("帐号已被禁用");
		}
		
		if(page != null && page > 0) SystemContext.setPageOffset((page-1)*rows);
		if(rows != null && rows > 0) SystemContext.setPageSize(rows);
		
		
		Pager<Role> pager = null;

		if(((boolean)session.getAttribute("isadmin"))) {
			pager = roleService.getAllPagerRoles(false);
		}else if(loginUser.getRole().getRoleName().equals("admin")) {
			pager = roleService.getAllPagerRoles(true);
		}
		
		
		SystemContext.removePageOffset();
		SystemContext.removePageSize();
		
		return pager;
	}
}
